package logic

import (
	"context"
	"rpc/internal/svc"
	"rpc/model/mysql"
	"rpc/model/redis"
	"rpc/pb/cart"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddGoodsLogic {
	return &AddGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddGoodsLogic) AddGoods(in *cart.AddGoodsRequest) (*cart.AddGoodsResponse, error) {
	// todo: add your logic here and delete this line

	g := mysql.Goods{
		GoodsTitle:  in.GoodsTitle,
		GoodsPrice:  float64(in.GoodsPrice),
		GoodsImg:    in.GoodsImg,
		GoodsStatus: in.GoodsStatus,
		GoodsStock:  in.GoodsStock,
	}
	err := g.Create()
	if err != nil {
		logx.Error("创建失败")
		return nil, err
	}

	redis.SyncGoodsStock(int64(g.ID), g.GoodsStock)

	return &cart.AddGoodsResponse{Success: true}, nil
}
